import {config} from "../../config.js"
import {HTTP} from "../../utils/http.js"
import Dialog from '../../miniprogram_npm/vant-weapp/dialog/dialog';
var http = new HTTP();
const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    imageSrc:"",//二维码地址
    phoneNum:"",
    shareInfo:null

  },
  updateCode:function(e){
      this.getImageCode();
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  
    this.setData({
      shareInfo: wx.getStorageSync("shareOptions")
    })
    this.getImageCode();
 
  },
  phoneFocus:function(){
    this.setData({
      isFocus1:true,
      isFocus2:false,
      isFocus3:false
    })
  },
  codeFocus:function(){
    this.setData({
      isFocus1: false,
      isFocus2: true,
      isFocus3: false
    })
  },
  indentiFocus:function() {
    this.setData({
      isFocus1: false,
      isFocus2: false,
      isFocus3: true
    })
  },
  phoneChange:function(e){
    this.setData({
      phoneNum:e.detail.value
    })
  },
  getPhoneSmsCode:function(){
    if (this.data.phoneNum == ""){
      Dialog.alert({
        title: '提示',
        message: "请输入的手机号。"
      });
      return;
    }
    if(this.data.phoneNum!="" && this.data.phoneNum.length!=11){
      Dialog.alert({
        title: '提示',
        message: "您输入的手机号有误。"
      });
      return;
    }
    let data = { "mobileNo": this.data.phoneNum, "templateNo": "SMS_157687052"};
    http.request({
        url:"/sys/sendmsg",
        method:"POST",
        data:data,
        success:function(data){
          wx.showToast({
            title: "已发送验证码。",
            icon: 'success',
            duration: 3000
          }) 
        }
      })
  },
  getImageCode:function(){
    var that = this;
    http.request({
      url: "/sys/generateImage" + "?" + Math.random(),
      success: function (data) {
        that.setData({
            imageSrc:data[0] ,  // data 为接口返回的base64字符串  
        })
        }
       
    });
  },
  onGotUserInfo:function(data){
  },
  formSubmit:function(e){
    console.log(app.userInfo)
    var info=e.detail.value;

    if (info.phonenum == "" || this.data.phoneNum.length != 11) {
      Dialog.alert({
        title: '提示',
        message: "您输入的手机号有误。"
      });
      return;
    }

    if (info.phoneidenti == "") {
      Dialog.alert({
        title: '提示',
        message: "请输入短信验证码。"
      });
      return;
    }

    let that = this;
    let smsCode={
      "verifyCode":info.phoneidenti
    }

    new Promise(function (resolve, reject) {

      http.request({
        url: "/sys/checkCaptcha",
        method: "POST",
        data: {
          "verCode": info.identicode
        },
        success: function (res) {
          console.log(res)
          if (res[0].result) {
            let data = {
              "mobile": info.phonenum
            }
            resolve(data)
          } else {
         
            Dialog.alert({
              message: '验证码输入错误。'
            }).then(() => {
              that.updateCode();
            });

            return;
          }
        }
      })

     
    }
    ).then(function(data){
      http.request({
        url: "/sys/verifymsgcode",
        method: "POST",
        data: smsCode,
        success: function (data) {
          console.log("验证成功", data)
          that.register(data);
        }
      })
    },function(error){

    })
   

   
  },
  register:function(){
      let that = this;
        wx.login({
          success: function (res) {
            let shareInfo = that.data.shareInfo;
            let userInfo=wx.getStorageSync("userInfo")
            let data = {
              "mobile": that.data.phoneNum,
              "code": res.code,
              "wechatProfileUrl": userInfo.avatarUrl
             };
            if(shareInfo!=null){
              if (shareInfo.inviteUserId!=""){
                data.inviteUserId = shareInfo.inviteUserId
              }
            }
            http.request({
              url: "/usr/register",
              method:"POST",
              data: data,
              success: function (data) {
                that.login(data);
              }
            })
          }
        });
  },
  login:function(data){
    let that = this;
    let updata = {
      "mobile": this.data.phoneNum
    }
    
    wx.login({
      success: function (res) {
        if (res.code) {
          //发起网络请求
          wx.showLoading({
            title: '登录中',
          })

          wx.request({
            url: config.api_base_url + '/sys/getwxinfo',
            data: {
              "mobile": that.data.phoneNum,
              "code": res.code
            },
            success: function (res) {
              let errCode = res.data.errCode;
              if (errCode == '0000') {
                let data = res.data.data[0];
                wx.setStorageSync("sessionid", res.header["Set-Cookie"]);
                    http.request({
                      url: "/usr/login",
                      method: "POST",
                      data: updata,
                      success: function (res) {
                        wx.redirectTo({
                          url: '/pages/certification/certification'
                        })
                      }
                    })
              }
            }
          });
        }
      }
    });
   
  }

})


